package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.dao.LiveCompanyMapper;
import com.example.blog.dao.LiveInfoMapper;
import com.example.blog.dto.AddCompanyReqDto;
import com.example.blog.dto.QueryCompanyResDto;
import com.example.blog.dto.QueryUserResDto;
import com.example.blog.dto.UpdateCompanyReqDto;
import com.example.blog.dto.base.IdRequestDto;
import com.example.blog.dto.base.PageIdReqDto;
import com.example.blog.entity.LiveCompany;
import com.example.blog.entity.LiveInfo;
import com.example.blog.entity.LiveUser;
import com.example.blog.response.CommonException;
import com.example.blog.service.LiveCompanyService;
import com.example.blog.service.LiveInfoService;
import com.example.blog.util.ParamUtil;
import com.example.blog.util.ToolsUtil;
import com.google.common.base.Objects;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author Admin
 */
@Service
public class LiveCompanyServiceImpl extends ServiceImpl<LiveCompanyMapper, LiveCompany> implements LiveCompanyService {

    private static final Boolean default_status = true;

    @Resource
    private LiveInfoService liveInfoService;

    @Resource
    private LiveCompanyMapper baseMapper;

    /**
     * 添加招聘信息
     * @param reqDto
     * @return
     */
    @Override
    @Transactional
    public Boolean addCompany(AddCompanyReqDto reqDto){
        LiveInfo liveInfo = liveInfoService.selectLiveInfoById(reqDto.getLiveInfoId());
        if(ParamUtil.empty(liveInfo)){
            throw new CommonException("查询不到该直播信息");
        }
        LiveCompany liveCompany = ToolsUtil.convertType(reqDto,LiveCompany.class);
        liveCompany.setId(ToolsUtil.getUUID());
        liveCompany.setCreateTime(LocalDateTime.now());
        if(reqDto.getDefaultStatus()){
            LiveCompany company = baseMapper.selectOne(Wrappers.lambdaQuery(LiveCompany.class)
                    .eq(LiveCompany::getLiveInfoId,reqDto.getLiveInfoId())
                    .eq(LiveCompany::getDefaultStatus,default_status)
                    .ne(LiveCompany::getStatus,9));
            if(ParamUtil.notEmpty(company)){
                company.setDefaultStatus(false);
                baseMapper.updateById(company);
            }
        }
        return baseMapper.insert(liveCompany) > 0;
    }

    /**
     * 修改招聘信息
     * @param reqDto
     * @return
     */
    @Override
    @Transactional
    public Boolean updateCompany(UpdateCompanyReqDto reqDto){
        LiveCompany oldCompany = baseMapper.selectById(reqDto.getId());
        if(ParamUtil.empty(oldCompany)){
            throw new CommonException("查询不到该招聘信息");
        }
        LiveCompany liveCompany = ToolsUtil.convertType(reqDto,LiveCompany.class);
        if(reqDto.getDefaultStatus()){
            LiveCompany company = baseMapper.selectOne(Wrappers.lambdaQuery(LiveCompany.class)
                    .eq(LiveCompany::getLiveInfoId,oldCompany.getLiveInfoId())
                    .eq(LiveCompany::getDefaultStatus,default_status)
                    .ne(LiveCompany::getStatus,9));
            if(ParamUtil.notEmpty(company)){
                company.setDefaultStatus(false);
                baseMapper.updateById(company);
            }
        }
        liveCompany.setCreateTime(LocalDateTime.now());
        return baseMapper.updateById(liveCompany) > 0;
    }

    /**
     * 删除招聘信息
     * @param requestDto
     * @return
     */
    @Override
    @Transactional
    public Boolean deleteCompany(IdRequestDto requestDto){
        LiveCompany liveCompany = baseMapper.selectById(requestDto.getId());
        //软删除
        liveCompany.setStatus(9);
        return baseMapper.updateById(liveCompany) > 0;
    }

    /**
     * 修改默认状态
     * @param requestDto
     * @return
     */
    @Override
    @Transactional
    public Boolean changeCompanyDefault(IdRequestDto requestDto){
        LiveCompany liveCompany = baseMapper.selectById(requestDto.getId());
        if(!liveCompany.getDefaultStatus()){
            liveCompany.setDefaultStatus(true);
            LiveCompany company = baseMapper.selectOne(Wrappers.lambdaQuery(LiveCompany.class).eq(LiveCompany::getDefaultStatus,default_status)
                    .eq(LiveCompany::getLiveInfoId,liveCompany.getLiveInfoId())
                    .ne(LiveCompany::getStatus,9));
            if(ParamUtil.notEmpty(company)){
                company.setDefaultStatus(false);
                baseMapper.updateById(company);
            }
        }else{
            liveCompany.setDefaultStatus(false);
        }
        return baseMapper.updateById(liveCompany) > 0;
    }

    /**
     * 分页查询招聘信息
     * @param reqDto
     * @return
     */
    @Override
    @Transactional
    public IPage<QueryCompanyResDto> selectCompany(PageIdReqDto reqDto){
        Page<LiveCompany> liveCompanyIPage = baseMapper.selectPage(reqDto.iPageInfo(), Wrappers.lambdaQuery(LiveCompany.class)
                .ne(LiveCompany::getStatus,9)
                .eq(LiveCompany::getLiveInfoId,reqDto.getId())
                .orderByDesc(LiveCompany::getCreateTime));
        IPage<QueryCompanyResDto> queryCompanyResDto = ToolsUtil.convertType(liveCompanyIPage,QueryCompanyResDto.class);
        return queryCompanyResDto;
    }

    @Override
    @Transactional
    public LiveCompany selectCompanyById(String id){
        return baseMapper.selectOne(Wrappers.lambdaQuery(LiveCompany.class).eq(LiveCompany::getId,id).ne(LiveCompany::getStatus,9));
    }

}
